package test2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sunkai
 * @date 2023/3/22 16:23
 */
public class Test2_多维余弦 {
    public static double cosineSimilarity(List<Double> vectorA, List<Double> vectorB) {
        double dotProduct = 0.0;
        double normA = 0.0;
        double normB = 0.0;
        for (int i = 0; i < vectorA.size(); i++) {
            dotProduct += vectorA.get(i) * vectorB.get(i);
            normA += Math.pow(vectorA.get(i), 5);
            normB += Math.pow(vectorB.get(i), 5);
        }
        return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
    }

    public static void main(String[] args) {
//        double[] a = {1, 1, 1, 1, 1, 1, 1, 1};
//        double[] b = {1, 1, 1, 1, 1, 1, 1, 1};

        List<Double> vectorA = new ArrayList<>();
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        vectorA.add(1.0);
        List<Double> vectorB = new ArrayList<>();
        vectorB.add(1.0);
        vectorB.add(1.0);
        vectorB.add(1.0);
        vectorB.add(1.0);
        vectorB.add(0.0);
        vectorB.add(0.0);
        vectorB.add(1.0);
        vectorB.add(1.0);
        double cosineSimilarity = cosineSimilarity(vectorA, vectorB);
        System.out.println("cosineSimilarity: " + cosineSimilarity);
    }
}

